import { Component, OnInit } from '@angular/core';
import { Platform, ModalController, LoadingController, ToastController, NavController } from '@ionic/angular';
import { BaseUI } from 'src/app/common/baseui';
import { RestService } from '../../servece/rest/rest.service';
import { load } from '@angular/core/src/render3';
import { async } from '@angular/core/testing';
import { Storage } from '@ionic/storage';
import { RegisterPage } from '../register/register.page';
import { ActivatedRoute, Router, NavigationEnd, NavigationStart, Params } from '@angular/router';

@Component({
  selector: 'app-login',
  templateUrl: './login.page.html',
  styleUrls: ['./login.page.scss'],
})
export class LoginPage extends BaseUI implements OnInit {

  root = RegisterPage;
  component = RegisterPage;
  mobile: any;
  password: any;
  errorMessage: any;

  constructor(
    public navCtrl: NavController,
    public platform: Platform, //
    public modalCtrl: ModalController, // 弹出页面
    public loadingCtrl: LoadingController, // 加载中遮罩
    public toastCtrl: ToastController, // 弹出提示框
    public restService: RestService, // 请求service
    private storage: Storage,
    private router: Router

  ) {
    super(); // 调用父类的构造函数
  }

  ngOnInit() {
  }

  login() {
    super.present('正在登陆...', this.loadingCtrl);
    // let loading = super.showLoading(this.loadingCtrl, '登陆中...');
    this.restService.login(this.mobile, this.password)
      .subscribe(
        async f => {
          const res = JSON.parse(f);
          if (res['Status'] === 'OK') {
            // 处理成功页面的跳转，也可以存储返回的 token
            this.storage.set('UserId', res['UserId'])
            // 成功,关闭“加载中”遮罩，关闭当前的这个弹出层；
            super.dismiss(this.loadingCtrl);
            this.disMiss();
          } else {
            super.dismiss(this.loadingCtrl);
            super.showToast(this.toastCtrl, res['StatusContent'])
          }
        },
        error => this.errorMessage = <any>error
      )
  }

  disMiss() {
    this.modalCtrl.dismiss({
      'dismissed': true
    });
  }

  pushRegisterPage() {
    this.disMiss();
    this.router.navigateByUrl('/register');
    // this.navCtrl.setDirection('root');
    // this.router.navigateByUrl('/register');

  }



}
